/*
 * SyncResource.java
 *
 * Created on April 12, 2007, 1:39 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.atomojo.auth.service.app;

import java.sql.SQLException;
import java.util.logging.Level;
import org.atomojo.app.client.XMLRepresentationParser;
import org.atomojo.auth.service.db.AuthDB;
import org.atomojo.auth.service.db.XML;
import org.restlet.data.CharacterSet;
import org.restlet.data.MediaType;
import org.restlet.data.Status;
import org.restlet.representation.Representation;
import org.restlet.representation.StringRepresentation;
import org.restlet.resource.ServerResource;

/**
 *
 * @author alex
 */
public class AllUsersResource extends ServerResource
{
   
   XMLRepresentationParser parser = XML.createParser();
   /** Creates a new instance of SyncResource */
   public AllUsersResource() {
      setNegotiated(false);
      parser.addAllowedElement(XML.USER_NAME);
   }
   
   public Representation get()
   {
      AuthDB db = (AuthDB)getRequest().getAttributes().get(AuthApplication.DB_ATTR);
      try {
         Representation entity = new DBIteratorRepresentation(MediaType.APPLICATION_XML,XML.USERS_NAME,db.getUsers(true),false);
         entity.setCharacterSet(CharacterSet.UTF_8);
         return entity;
      } catch (SQLException ex) {
         getContext().getLogger().log(Level.SEVERE,"Cannot get users from database.",ex);
         getResponse().setStatus(Status.SERVER_ERROR_INTERNAL);
         return new StringRepresentation("Exception during processing, see logs.");
      }
   }
   
}
